/*
 * File: TVSeries.java
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tvseries;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.ObjectNotFoundException;

import tvseries.entity.Episode;
import tvseries.entity.Series;
import tvseries.utils.DataUtil;
import tvseries.utils.HibernateUtil;

/**
 * File for handling command line interface.
 * 
 * @author nish.naidoo@gmail.com
 * 
 */

public class TVSeries
{
	public static final Logger log = Logger.getLogger(TVSeries.class);

	public static void main(String[] args) throws Exception
	{
		if (args.length == 0)
		{
			showUsage();
			return;
		}

		if (args[0].equalsIgnoreCase("loadSeriesListing"))
		{
			log.info("Loading series listing... " + args[0]);
			DataUtil.loadSeriesListing();
			log.info("Loading series listing completed");
			return;
		}

		if (args[0].equalsIgnoreCase("listSeries"))
		{
			log.info("Loading series listing... " + args[0]);
			listSeries();
			log.info("Loading series listing completed");
			return;
		}

		if (args[0].equalsIgnoreCase("updateSeries"))
		{
			if (args.length > 1 && args[1].matches("\\d{6,10}"))
			{
				log.info("Updating series: " + args[1]);
				updateSeries(args[1]);
				log.info("Updating series completed");
			}
			else
			{
				System.out.println("updateSeries requires a seriesID parameter");
			}
			return;
		}

		if (args[0].equalsIgnoreCase("listEpisodes"))
		{
			if (args.length > 1 && args[1].matches("\\d{6,10}"))
			{
				log.info("Listing episodes: " + args[1]);
				listEpisodes(args[1]);
				log.info("Listing episodes completed.");
			}
			else
			{
				System.out.println("listEpisodes requires a seriesID parameter");
			}
			return;
		}

		showUsage();
	}

	private static void showUsage()
	{
		System.out.println("  run [command]");
		System.out.println("    command:");
		System.out.println("      loadSeriesListing      populate new series from series_listing.txt");
		System.out.println("      updateSeries seriesId  populate the database with given seriesId");
		System.out.println("      listSeries             list the series titles and ids");
	}
	
	private static void listSeries()
	{
		List<Series> series = DataUtil.getSeries();
		System.out.println("Listing TV Series\n");
		for (Series s : series)
		{
			System.out.println(s.getId() + " " + s.getTitle());
		}
	}
	
	private static void updateSeries(String seriesId)
	{
		try
		{
			HibernateUtil.load(Series.class, seriesId);
			DataUtil.updateSeries(seriesId);
		}
		catch (ObjectNotFoundException onfe)
		{
			System.out.println("Could not find this series id in the database: " + seriesId);
		}
		catch (Exception e)
		{
			System.out.println("Error occurred updating series: " + e.getMessage());
		}
	}
	
	private static void listEpisodes(String seriesId)
	{
		try
		{
			Series series = (Series) HibernateUtil.load(Series.class, seriesId);
			Episode[] episodes = series.getEpisodes().toArray(new Episode[series.getEpisodes().size()]);
			Arrays.sort(episodes);
			System.out.println("Episode listing for: " + series.getTitle());
			for (Episode e : episodes)
			{
				String episode = (e.getEpisode() < 10) ? "0" + e.getEpisode() : String.valueOf(e.getEpisode());
				String season = (e.getSeason() < 10) ? "0" + e.getSeason() : String.valueOf(e.getSeason());
				System.out.print("[" + e.getId()+ "] ");
				System.out.print(season + "x" + episode + " ");
				System.out.print(e.getTitle());
				System.out.println();
			}			
		}
		catch (ObjectNotFoundException onfe)
		{
			System.out.println("Could not find this series id in the database: " + seriesId);
		}
		catch (Exception e)
		{
			System.out.println("Error occurred listing series: " + e.getMessage());
		}
	}
}
